package com.mvc.business.service;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;

import com.mvc.business.model.QuotationExtraItem;
import com.mvc.framework.model.Auditable;
import com.mvc.framework.service.BaseService;

@Service
public class QuotationExtraItemManager extends BaseService<QuotationExtraItem, Integer> {

	public List<QuotationExtraItem> getQuotationExtraItemByQuotationId(int quotationId, Integer type) {
		QuotationExtraItem orderItem = new QuotationExtraItem();
		orderItem.setQuotationId(quotationId);
		orderItem.setRecStatus(Auditable.REC_STATUS_VALID);
		orderItem.setType(type);
		return getHibernateTemplate().findByExample(orderItem);
	}

	public void deleteByQuotationId(int quotationId) {
		deleteByWhere("quotationId=" + quotationId);
	}

	public void saveQuotationExtraItem(QuotationExtraItem orderItem, String operation) throws Exception {
		if (operation.equals("del")) {
			super.delete(orderItem.getId());
		} else if (operation.equals("add")) {
			orderItem.setId(null);
			super.save(orderItem);
		} else if (operation.equals("edit")) {
			super.save(orderItem);
		}
	}

	public void updateQuotationId(Integer oldOrderId, Integer newOrderId) {
		Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Query query = session.createQuery("update " + QuotationExtraItem.class.getName() + " set quotationId = " + newOrderId
		        + " where quotationId = " + oldOrderId);
		query.executeUpdate();
	}
}
